package com.yuanjiu.service.yuanjiu;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yuanjiu.dao.yuanjiu.ProductDao;
import com.yuanjiu.pojo.yuanjiu.CommentObject;
import com.yuanjiu.pojo.yuanjiu.JiuObject;
import com.yuanjiu.vo.yuanjiu.PageVo;
import com.yuanjiu.vo.yuanjiu.ProductVo;

@Service
public class ProductService {
	@Autowired
	private ProductDao productDao;

	public PageVo findProductListByFilter(ProductVo vo, PageVo voParam) {
		//分页
		PageHelper.startPage(voParam.getCurrentPage(), voParam.getPageSize());
		List<JiuObject> list = productDao.findProductListByFilter(vo); 
		PageInfo<JiuObject> pageInfo = new PageInfo<>(list);
		
		PageVo pageVo = new PageVo(); 
		pageVo.setCurrentPage(voParam.getCurrentPage());
		pageVo.setPageSize(voParam.getPageSize()); 
		pageVo.setTotal(pageInfo.getTotal());
		pageVo.setOrder(voParam.getOrder());
		pageVo.setOrderBy(voParam.getOrderBy());
		List<JiuObject> jiuObjects = pageInfo.getList();
		for(JiuObject jiuObject : jiuObjects){
			String imgs = jiuObject.getPic();
			if(imgs != null){
				//1919图片Url特殊处理
				if(jiuObject.getWebCode() != null && jiuObject.getWebCode().equals("2")){
					jiuObject.setPic(jiuObject.getPic().split("\\?")[0]);
					continue;
				}
				String img = imgs.split(",")[0];
				jiuObject.setPic(img);
			}
		}
		pageVo.setList(jiuObjects);
		return pageVo;
	}

	public JiuObject getProductById(JiuObject jiuObject) {
		return productDao.getProductById(jiuObject);
	}

	public List<CommentObject> getCommentByGoodsId(JiuObject jiuObject) {
		return productDao.getCommentByGoodsId(jiuObject);
	}

	public List<JiuObject> findRecentHistory(List<String> idList) {
		return productDao.findRecentHistory(idList);
	}
	
	
}
